import { Injectable, NestInterceptor } from "@nestjs/common";
import { ExecutionContext } from "@nestjs/common";
import { CallHandler } from "@nestjs/common";
import { Observable } from "rxjs";
import { tap } from "rxjs";

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const url = context.switchToHttp().getRequest().url;

    console.log(`<<<<<<<<<< Before URL: [${url}] >>>>>>>>>>>>`);
    const now = Date.now();
    return next.handle().pipe(tap(()=> {
      return console.log(`<<<<<<<<<< After URL: [${url}] ${Date.now() - now}ms >>>>>>>>>>>>`);
    }))
  }
}